package com.kly.cashmall.widget.refresh;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import androidx.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kly.cashmall.AppApplication;
import com.kly.cashmall.R;
import com.scwang.smart.refresh.layout.api.RefreshFooter;
import com.scwang.smart.refresh.layout.api.RefreshKernel;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.RefreshState;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;


/**
 * @author chenwei
 * @date 2018/1/22
 */
public class SimpleRefreshFooter extends LinearLayout implements RefreshFooter {

  protected RefreshKernel mRefreshKernel;
  protected int mBackgroundColor;
  protected int mFinishDuration = 400;

  private ImageView refreshIcon;
  private TextView refreshText;

  private String refreshStr = AppApplication.getInstance().getString(R.string.srl_footer_pulling);
  private String refreshNoMoreStr = AppApplication.getInstance().getString(R.string.srl_footer_nothing);
  private int refreshDrawableResource = R.mipmap.ic_refresh_loading;

  public SimpleRefreshFooter(Context context) {
    this(context, null);
  }

  public SimpleRefreshFooter(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  public SimpleRefreshFooter(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    LayoutInflater.from(context).inflate(R.layout.layout_simple_refresh_header, this, true);

    refreshIcon = findViewById(R.id.refresh_icon);
    refreshText = findViewById(R.id.refresh_txt);
  }

  @NonNull
  @Override
  public View getView() {
    return this;
  }

  @Override
  public SpinnerStyle getSpinnerStyle() {
    return SpinnerStyle.Translate;
  }

  @Override
  public void setPrimaryColors(int... colors) {
    if (colors.length > 0) {
      if (!(getBackground() instanceof BitmapDrawable)) {
        setPrimaryColor(colors[0]);
      }
    }
  }

  @Override
  public void setBackgroundColor(int color) {
    super.setBackgroundColor(color);
    mBackgroundColor = color;
  }

  private void setPrimaryColor(int primaryColor) {
    setBackgroundColor(primaryColor);
  }

  @Override
  public void onInitialized(RefreshKernel kernel, int height, int extendHeight) {
    mRefreshKernel = kernel;
    mRefreshKernel.requestDrawBackgroundFor(this, mBackgroundColor);
  }

  @Override
  public void onMoving(boolean isDragging, float percent, int offset, int height,
                       int maxDragHeight) {

  }

  @Override
  public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

  }

  @Override
  public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {

  }

  @Override
  public void onStartAnimator(@NonNull RefreshLayout layout, int height, int extendHeight) {

  }

  @Override
  public int onFinish(@NonNull RefreshLayout layout, boolean success) {
    return mFinishDuration;
  }

  @Override
  public boolean isSupportHorizontalDrag() {
    return false;
  }

  @Override
  public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState,
                             @NonNull RefreshState newState) {

    if (isNoMoreData) {
      changeRefreshIconStatus(false);
      return;
    }

    if (refreshIcon.getAnimation() == null) {
      refreshIcon.setImageResource(refreshDrawableResource);
      refreshText.setText(refreshStr);
      changeRefreshIconStatus(true);
    }
  }

  private void changeRefreshIconStatus(boolean loading) {
    if (loading) {
      if (refreshIcon.getAnimation() == null) {
        RotateAnimation rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f,
            Animation.RELATIVE_TO_SELF, 0.5f);
        LinearInterpolator lin = new LinearInterpolator();
        rotate.setInterpolator(lin);
        rotate.setDuration(800);
        rotate.setRepeatCount(-1);
        rotate.setFillAfter(true);
        rotate.setStartOffset(0);
        refreshIcon.startAnimation(rotate);
      }
    } else {
      refreshIcon.setRotation(0);
      if (refreshIcon.getAnimation() != null) {
        refreshIcon.clearAnimation();
      }
    }
  }

  public SimpleRefreshFooter setRefreshNoMoreStr(String refreshNoMoreStr) {
    this.refreshNoMoreStr = refreshNoMoreStr;
    refreshText.setText(refreshNoMoreStr);
    return this;
  }

  public SimpleRefreshFooter setRefreshStr(String refreshStr) {
    this.refreshStr = refreshStr;
    refreshText.setText(refreshStr);
    return this;
  }

  public SimpleRefreshFooter setRefreshDrawableIcon(int drawableIcon) {
    this.refreshDrawableResource = drawableIcon;
    refreshIcon.setImageResource(drawableIcon);
    return this;
  }

  @Override
  protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();
  }

  private boolean isNoMoreData = false;

  @Override
  public boolean setNoMoreData(boolean noMoreData) {
    this.isNoMoreData = noMoreData;
    refreshIcon.setVisibility(noMoreData ? GONE : VISIBLE);
    if (noMoreData) {
      changeRefreshIconStatus(false);
      refreshText.setText(refreshNoMoreStr);
    }
    return noMoreData;
  }
}
